﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Controls;
using Infov1.Models;
using Infov1.ViewModels.WorkRecord;
using GalaSoft.MvvmLight.Messaging;

namespace Infov1.Views.WorkRecord
{
    /// <summary>
    /// 工作记录视图的交互逻辑
    /// </summary>
    public partial class WorkRecordView : UserControl
    {
        /// <summary>
        /// 构造函数：初始化工作记录视图
        /// </summary>
        public WorkRecordView()
        {
            // 初始化界面组件
            InitializeComponent();
          
            // 设置数据上下文为工作记录ViewModel
            this.DataContext = new WorkRecordViewModel();
            //关闭时取消注册
            this.Unloaded += WorkRecordView_Unloaded;
            //接收注册消息
            Messenger.Default.Register<string>(this, "SetFocus", setFous);
        }

       
        //接收到消息设置焦点
        private void setFous(string message)
        {
            // 这里会收到 设置焦点的消息
            customerSearchBar.Focus();
        }
        /// <summary>
        /// 搜索框开始搜索事件处理方法
        /// </summary>
        /// <param name="sender">触发事件的搜索框控件</param>
        /// <param name="e">事件参数</param>
        private void SearchBar_SearchStarted(object sender, EventArgs e)
        {
            // 将sender转换为SearchBar类型
            var searchBar = sender as SearchBar;
            if (searchBar == null) return;

            try
            {
                // 获取搜索框中的文本
                var searchText = searchBar.Text;

                // 创建客户选择对话框
                var dialog = new CustomerSelectDialog();
                // 创建客户选择ViewModel并传入搜索文本
                var selectViewModel = new CustomerSelectViewModel(searchText);
                // 设置对话框的数据上下文
                dialog.DataContext = selectViewModel;

                // 显示对话框并等待结果
                // ShowDialog()返回true表示用户点击了确定按钮
                if (dialog.ShowDialog() == true && selectViewModel.SelectedCustomer != null)
                {
                    // 获取主窗口的ViewModel
                    if (this.DataContext is WorkRecordViewModel mainViewModel)
                    {
                        // 更新主窗口中的客户信息
                        UpdateCustomerInfo(mainViewModel, selectViewModel.SelectedCustomer);
                    }
                }
            }
            catch (Exception ex)
            {
                // 错误处理
                Growl.Error($"选择客户时发生错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 更新主窗口中的客户信息
        /// </summary>
        /// <param name="mainViewModel">主窗口ViewModel</param>
        /// <param name="selectedCustomer">选中的客户信息</param>
        private void UpdateCustomerInfo(WorkRecordViewModel mainViewModel, CustomerInfoModel selectedCustomer)
        {
            mainViewModel.CustomerCode = selectedCustomer.CustomerCode;    // 更新客户编码
            mainViewModel.CustomerName = selectedCustomer.CompanyName;     // 更新客户名称
            mainViewModel.SearchText = selectedCustomer.CustomerCode;      // 更新搜索文本
            mainViewModel.KhId = selectedCustomer.Id;                                     //更新用户id
        }

        // 记得在窗口关闭时取消注册
        private void WorkRecordView_Unloaded(object sender, RoutedEventArgs e)
        {
            // 取消所有消息注册
            Messenger.Default.Unregister(this);

            // 取消事件订阅
            this.Unloaded -= WorkRecordView_Unloaded;
        }
    }
}